package Admin;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Date;
import java.util.Properties;
import java.awt.Toolkit;
import java.awt.Color;

public class QnAMail extends JFrame {

	private JPanel contentPane;
	private JTextField receiver;
	private JTextField sender;
	private JTextField subject;
	private JScrollPane scrollPane;
	private JTextArea textArea;
	private JButton btnNewButton;
	private JLabel lblNewLabel;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	QnAVo v;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					QnAMail frame = new QnAMail();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public QnAMail(QnAVo v) {
		this();
		this.v = v;
		
		receiver.setText(v.getMail());
		subject.setText("답변 : "+v.getSubject());
		sender.setText("shinjaeup@gmail.com");
		
	}
	public QnAMail() {
		setBackground(Color.WHITE);
		setTitle("QnA");
		setIconImage(Toolkit.getDefaultToolkit().getImage(QnAMail.class.getResource("/javax/swing/plaf/metal/icons/ocean/upFolder.gif")));
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 376, 467);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getReceiver());
		contentPane.add(getSender());
		contentPane.add(getSubject());
		contentPane.add(getScrollPane());
		contentPane.add(getBtnNewButton());
		contentPane.add(getLblNewLabel());
		contentPane.add(getLabel());
		contentPane.add(getLabel_1());
		contentPane.add(getLabel_2());
	}
	private JTextField getReceiver() {
		if (receiver == null) {
			receiver = new JTextField();
			receiver.setBounds(64, 21, 179, 21);
			receiver.setColumns(10);
		}
		return receiver;
	}
	private JTextField getSender() {
		if (sender == null) {
			sender = new JTextField();
			sender.setColumns(10);
			sender.setBounds(64, 52, 179, 21);
		}
		return sender;
	}
	private JTextField getSubject() {
		if (subject == null) {
			subject = new JTextField();
			subject.setColumns(10);
			subject.setBounds(64, 83, 284, 21);
		}
		return subject;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(64, 114, 284, 263);
			scrollPane.setViewportView(getTextArea());
		}
		return scrollPane;
	}
	private JTextArea getTextArea() {
		if (textArea == null) {
			textArea = new JTextArea();
		}
		return textArea;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("보내기");
			btnNewButton.setBackground(Color.WHITE);
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					
					
					String senderadd = sender.getText();
					String receiveradd = receiver.getText();
					String a_subject = subject.getText();
					String a_content = textArea.getText();
					String server = "localhost";
					
//					mail();
					
					
					QnAVo vo = new QnAVo();
					
					vo.setMid("admin");
					vo.setSubject(a_subject);
					vo.setContent(a_content);
					vo.setMail(senderadd);
					vo.setCategory(v.getCategory());
					
					ManageDTO dto = new ManageDTO();
					ManageDTO dto2 = new ManageDTO();
					if(dto.insertAnswer(vo, v.getRno())){
						if(dto2.updateconfirm(v.getRno())){
						JOptionPane.showMessageDialog(QnAMail.this, "메일전송 완료");
						dispose();
						}else{
							JOptionPane.showMessageDialog(QnAMail.this, "메일전송 완료/디비 업데이트 오류");	
						}
						
					}else{
						JOptionPane.showMessageDialog(QnAMail.this, "메일전송 완료/디비 저장 오류");
					}
					
					
					
				
					
				}
			});
			btnNewButton.setFont(new Font("Gulim", Font.PLAIN, 12));
			btnNewButton.setBounds(251, 387, 97, 23);
		}
		return btnNewButton;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("받는이");
			lblNewLabel.setFont(new Font("Gulim", Font.PLAIN, 12));
			lblNewLabel.setBounds(12, 24, 46, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("보내는이");
			label.setFont(new Font("Gulim", Font.PLAIN, 12));
			label.setBounds(12, 55, 48, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("제목");
			label_1.setFont(new Font("Gulim", Font.PLAIN, 12));
			label_1.setBounds(12, 86, 46, 15);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("내용");
			label_2.setFont(new Font("Gulim", Font.PLAIN, 12));
			label_2.setBounds(12, 119, 46, 15);
		}
		return label_2;
	}
	
	public void mail(){
		String senderadd = sender.getText();
		String receiveradd = receiver.getText();
		String a_subject = subject.getText();
		String a_content = textArea.getText();
		String server = "localhost";
		
		try{
		
		Properties properties = new Properties();
		properties.setProperty("mail.stmp.host", server);
		Session s = Session.getDefaultInstance(properties,null);
		Message msg = new MimeMessage(s);
		
		Address sender_add = new InternetAddress(senderadd);
		Address receiver_add = new InternetAddress(receiveradd);
		
		msg.setHeader("content-type","text/html;charset=utf-8");
		msg.setFrom(sender_add);
		msg.addRecipient(Message.RecipientType.TO, receiver_add);
		msg.setSubject(a_subject);
		msg.setContent(a_content, "text/html;charset=utf-8");
		msg.setSentDate(new Date());
		
		Transport transport = s.getTransport("smtp");
		transport.connect(server, null, null);
		transport.sendMessage(msg, msg.getAllRecipients());
		transport.close();
		}catch(Exception e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(QnAMail.this, "메일전송 오류");
		}
	}
	
}
